import React, { useCallback, useState } from 'react';

const defaultPagination = {
  current: 1,
  pageSize: 10,
};

const usePage = (config: any = defaultPagination) => {
  const [Pagination, setPagination] = useState({
    current: 1,
    pageSize: 10,
  });
  const pagination: any = useCallback(() => {
    return {
      ...defaultPagination,
      ...config,
      current: Pagination.current,
      pageSize: Pagination.pageSize,
      onChange: (page: number, pageSize: number) => {
        if (config.onChange) {
          config.onChange(page, pageSize);
        }
        setPagination({ current: page, pageSize });
      },
    };
  }, [config, Pagination.current]);

  return pagination();
};

export default usePage;